package com.glory.huaweipda.view

import android.annotation.SuppressLint
import android.app.ActivityManager
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.widget.Toast
import com.glory.huaweipda.R
import com.glory.huaweipda.adapter.SimpleAdapter
import com.glory.huaweipda.base.MyApp
import com.glory.huaweipda.bean.EQPLotInfoBean
import com.glory.huaweipda.viewmodel.GetEQPLogModel
import com.glory.mvvmnet.base.BaseActivity
import kotlinx.android.synthetic.main.activity_log.*

class GetEQPLogActivity : BaseActivity<GetEQPLogModel>() {

    var type: String = ""
    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
            when (msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    type = "oo"
                    val intent = Intent()
                    intent.putExtra("showPlugin", "errorLog")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
                    intent.setClass(this@GetEQPLogActivity, clazz)
                    startActivity(intent)
                }
            }
        }
    }

    override fun layoutId(): Int {
        return R.layout.activity_log
    }

    override fun initData() {

    }
    var mEqpidEdit:String = ""
    override fun initView(savedInstanceState: Bundle?) {

        val instance = MyApp.instance
        instance!!.handler = handler
        val mfgItems = viewModel.mfgItems
        val simpleAdapter = SimpleAdapter(this,mfgItems)
        recycler_batch.adapter = simpleAdapter
//        val bind : ActivityLogBinding =  DataBindingUtil.setContentView(this,R.layout.activity_log)
//        bind.getEQPLogModel = viewModel
        viewModel.initAssets(this)
        recycler_batch.setNestedScrollingEnabled(false)

        viewModel.defUI.callObserve.observe(this){
            if ("oo".equals(it.type))it.type = -1
            when(it.type){
                viewModel.Batch_TrackIn_Model ->{
                    if ("com.glory.huaweipda.view.SkipDialogActivity".equals(getTopActivity(applicationContext))){
                        SkipDialogActivity.getActivity()
                    }
                    val eqpLotInfoBean = it.item as EQPLotInfoBean.EQPLotInfoBeanItem
                    equipment_code.setText(eqpLotInfoBean.eqpid)
                    batch_code.setText(eqpLotInfoBean.lotid)
                    status.setText(eqpLotInfoBean.eqpstate)
                    program.setText(eqpLotInfoBean.recipeName)
                    current_site.setText(eqpLotInfoBean.stepName)
                    next_site.setText(eqpLotInfoBean.stepName)
                }
                0 ->{
                    if ("com.glory.huaweipda.view.SkipDialogActivity".equals(getTopActivity(applicationContext))){
                        SkipDialogActivity.getActivity()
                    }
                    simpleAdapter.notifyDataSetChanged()
                    viewModel.defaultGetEQPLog(mEqpidEdit)
                }
            }
        }
        arrival.setOnClickListener{
            mEqpidEdit = eqpidEdit.text.toString()
            if (eqpidEdit.equals("")){
                Toast.makeText(this, "请输入设备编号", Toast.LENGTH_SHORT).show()
            }
            viewModel.defaultGetEQPLotInfo(mEqpidEdit)
            viewModel.defaultGetEQPLog(mEqpidEdit)
            line.visibility = View.VISIBLE
//            val intent = Intent()
//            intent.putExtra("showPlugin", "set_number")
//            val clazz = Class.forName("com.glory.huaweipda.view.SkipDialogActivity")
//            intent.setClass(this, clazz)
//            startActivity(intent)
        }

        eqpidEdit.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                val toString = s.toString()
                if (toString.indexOf("\r") >= 0 || toString.indexOf("\n") >= 0) {
                    eqpidEdit.setText(toString.replace("\r", "").replace("\n", ""));
                }
            }

        })

        ivBack.setOnClickListener{
            finish()
        }

    }

    fun getTopActivity(context: Context): String {
        val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
        val cn = am.getRunningTasks(1)[0].topActivity
        Log.d("Chunna.zheng", "cls:" + cn!!.className) //包名加类名
        return cn.className
    }
}